package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCardMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;


@Service
@Log4j2
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCardMapper shoppingCardMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;


    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long id = BaseContext.getCurrentId();
        shoppingCart.setUserId(id);


        List<ShoppingCart> list = shoppingCardMapper.getShoppingCardById(shoppingCart);
        if (list != null && !list.isEmpty()) {
            ShoppingCart cart = list.get(0);
            shoppingCart.setNumber(cart.getNumber() + 1);
            shoppingCardMapper.updateSpCardNumber(shoppingCart);
        } else {
            Long dishId = shoppingCart.getDishId();
            if (dishId != null) {
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
            } else {
                Long setmealId = shoppingCart.getSetmealId();
                Setmeal setmeal = setmealMapper.getBySetmealId(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCardMapper.insert(shoppingCart);
        }
    }

    @Override
    public List<ShoppingCart> getByUserIdWithSpCard() {
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        return shoppingCardMapper.getShoppingCardById(shoppingCart);
    }

    @Override
    @Transactional
    public void deSpCard(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        String dishFlavor = shoppingCartDTO.getDishFlavor();
        Long dishId = shoppingCartDTO.getDishId();

        Long id = BaseContext.getCurrentId();

        shoppingCart.setUserId(id);
        shoppingCart.setDishFlavor(dishFlavor);
        shoppingCart.setDishId(dishId);

        List<ShoppingCart> list = shoppingCardMapper.getShoppingCardById(shoppingCart);
        ShoppingCart cart = list.get(0);
        ShoppingCart cart1 = ShoppingCart.builder().userId(BaseContext.getCurrentId()).dishId(dishId).dishFlavor(dishFlavor).build();
        if (cart == null || list.get(0).getNumber() == 1) {
            //仅剩一个删除数据
            shoppingCardMapper.delete(cart1);
        } else {
            cart1.setNumber(cart.getNumber() - 1);
            shoppingCardMapper.updateSpCardNumber(cart1);
        }
    }

    @Override
    public void deAllSpCard() {
        Long userId = BaseContext.getCurrentId();
        shoppingCardMapper.deleteAll(userId);
    }
}
